package com.briup.estore.userAPI.web.listener;

import com.briup.estore.common.bean.Banner;
import com.briup.estore.common.bean.Shop;
import com.briup.estore.common.bean.VO.CategoryVO;
import com.briup.estore.common.service.IBannerService;
import com.briup.estore.common.service.ICategoryService;
import com.briup.estore.common.service.IShopService;
import com.briup.estore.common.util.DateUtil;
import com.briup.estore.common.util.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.List;

/**
 * @author adam
 * @date 2022/1/13
 */
@Component
public class ApplicationListener implements ServletContextListener {
    @Autowired
    private IShopService shopService;
    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private IBannerService bannerService;
    @Autowired
    private RedisCache redisCache;


    @Value("${nginx.path}")
    private String path;

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        List<Shop> shops = shopService.findAllShops();
        ServletContext application = sce.getServletContext();
        List<CategoryVO> categoryVOList = null;
        try {
            categoryVOList = categoryService.findAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<Banner> banners = bannerService.findByStatus(true);
        application.setAttribute("banners", banners);
        application.setAttribute("shops", shops);
        application.setAttribute("categories", categoryVOList);
        application.setAttribute("path", path);
        List<Shop> discountList = shopService.findDiscount();
        application.setAttribute("discountList", discountList);

    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
//        Integer online = redisCache.getCacheObject("online");
//        Integer visitors = redisCache.getCacheObject("visitors");

    }
}
